/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {


        var doctor_id= $("#doctor_id option:selected").val()=='-1'?"":$("#doctor_id option:selected").val()
        var hospital_id= $("#hospital_id option:selected").val()=='-1'?"":$("#hospital_id option:selected").val()
        var status=$("#status option:selected").val()=='-1'?"":$("#status option:selected").val()

        gettable(doctor_id,hospital_id,status);

        $("#select-btn").bind("click", function () {

            var doctor_id= $("#doctor_id").find("option:selected").val()=='-1'?"":$("#doctor_id").find("option:selected").val()
            var hospital_id= $("#hospital_id").find("option:selected").val()=='-1'?"":$("#hospital_id").find("option:selected").val()
            var status=$("#status").find("option:selected").val()=='-1'?"":$("#status").find("option:selected").val();
            gettable(doctor_id,hospital_id,status);
        });


        //获得订单总数
        var pamars={};
        pamars.doctor_id='';
        pamars.hospital_id='';
        pamars.status='';
        IO.post("/api/consults/search",pamars,function(d){
            if(d.code=="0"){
                var  count= d.data.total;
                $("#count").text(count);
            }
        })
        //获得所有医院
        IO.post("/api/hospitalsAll",pamars,function(d){
            if(d.code=="0"){
                $("#hospital_id").empty();
                $("#hospital_id").append("<option value='-1' selected>医院</option>");
                for (var i=0;i< d.data.length;i++){
                    $("#hospital_id").append("<option value='"+d.data[i].hospital_id+"'>"+d.data[i].hospital_name+"</option>");
                }
            }
        })

        //选择医院,所在医院医生联动
        $("#hospital_id").on("change",function(){
            $("#doctor_id").empty();
            $("#doctor_id").append("<option value='-1' selected>医生名称</option>");
           var hospital_id= $("#hospital_id option:selected").val()//获得医院id
            var pamars={};
            pamars.hospital_id=hospital_id
            IO.post("/api/hospitalsAllDoctor",pamars,function(d){
                if(d.code=="0"){
                    for (var i=0;i< d.data.length;i++){
                        $("#doctor_id").append("<option value='"+d.data[i].doctor_id+"'>"+d.data[i].realname+"</option>");
                    }
                }
            })

        });

    });

    function gettable(doctor_id,hospital_id,status){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '用户昵称',
                dataIndex: 'nickname',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" + data[datarow]['user'].nickname + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '医生姓名',//-----------------------
                dataIndex: 'realname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow]['doctor'].realname + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '咨询费用',
                dataIndex: 'consult_fee',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].consult_fee + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '咨询开始时间',
                dataIndex: 'createdDate',//-----------------------------------
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].createdDate + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '咨询结束时间',
                dataIndex: 'end_date',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].end_date + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '问答评分',
                dataIndex: 'score',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].score+ "</span>";

                }
            },
            {
                title: '状态',
                dataIndex: 'status',
                dataSource: function (data, datarow, gridobj, current_column) {
                    //alert(data[datarow].status)
                    if(data[datarow].status==1){
                        return "<span>" + '进行中' + "</span>";
                    }
                    else {
                        return "<span>" + '结束' + "</span>";
                    }
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            }

        ];
        //数组初始化
        function dataInit(doctor_id,hospital_id,status) {

            grid = new Grid("/api/consults/search", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(doctor_id,hospital_id,status), 10);
            //初始化
            grid.Init();
        }



        //获取输入框内容
        function get_input_param(doctor_id,hospital_id,status) {

            var param="doctor_id="+doctor_id+"&hospital_id="+hospital_id+"&status="+status+"";
            return param;
        }
        dataInit(doctor_id,hospital_id,status);
    }


});